<html>
<head>
<link rel='stylesheet' href='../../js/sh/SyntaxHighlighter.css' type='text/css' />
<script src='../../js/sh/shCore.js'></script>
<script src='../../js/sh/shBrushJava.js'></script>
<style>
* {
font-family:Courier New,monospace;
  padding: 0;
  margin: 0;
  white-space: nowrap;
  font-size: 11px;
}
.dp-highlighter {
  white-space: nowrap;
  overflow: visible;
  width: 600px;
  font-size: 11px;
  font-family:Courier New,monospace;
}
</style>
</head>
<body>
<textarea name='code' class='java:nogutter' rows='15' cols='120'>
/*
 * SmartGWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * SmartGWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  SmartGWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.TopOperatorAppearance;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.FilterBuilder;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.FilterCriteriaFunction;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.grid.*;
import com.smartgwt.client.widgets.layout.VStack;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.sample.showcase.client.data.WorldXmlDS;
import com.smartgwt.sample.showcase.client.data.EmployeeData;
import com.smartgwt.sample.showcase.client.data.SupplyCategoryXmlDS;
import com.smartgwt.sample.showcase.client.data.ItemSupplyXmlDS;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.HashMap;

public class GridDependentSelectsSample implements EntryPoint {

    public void onModuleLoad() {
        VLayout layout = new VLayout(15);
        Label localDataLabel = new Label("Local Data");
        localDataLabel.setWidth("90%");
        localDataLabel.setHeight(25);
        localDataLabel.setBaseStyle("exampleSeparator");
        layout.addMember(localDataLabel);


        final ListGrid localDataGrid = new ListGrid();
        localDataGrid.setWidth(500);
        localDataGrid.setHeight(200);
        localDataGrid.setCanEdit(true);
        localDataGrid.setData(EmployeeData.getRecords());

        ListGridField employeeField = new ListGridField("employee", "Name");
        employeeField.setCanEdit(false);

        ListGridField divisionField = new ListGridField("division", "Division");
        SelectItem divisionSelectItem = new SelectItem();
        divisionSelectItem.setValueMap("Marketing", "Sales", "Services");
        divisionSelectItem.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent event) {
                //calling 'setValueMap()' will force the 'getEditorValueMap' method to be re-evaluated for the department field
                localDataGrid.setValueMap("department", new LinkedHashMap());
            }
        });
        divisionField.setEditorType(divisionSelectItem);

        ListGridField departmentField = new ListGridField("department", "Department");

        final Map<String, String[]> departments = new HashMap<String, String[]>();
        departments.put("Marketing", new String[]{"Advertising", "Community Relations"});
        departments.put("Sales", new String[]{"Channed Sales", "Direct Sales"});
        departments.put("Manufacturing", new String[]{"Design", "Development", "QA"});
        departments.put("Services", new String[]{"Support", "Consulting"});

        SelectItem departmentSelectItem = new SelectItem();
        departmentSelectItem.setAddUnknownValues(false);
        departmentField.setEditorValueMapFunction(new EditorValueMapFunction() {
            public Map getEditorValueMap(Map values, ListGridField field, ListGrid grid) {
                String division = (String) values.get("division");
                String[] divisions = departments.get(division);

                //convert divisions into ValueMap. In this case we simply create a Map with same key -> value since
                //stored value is the same as user displayable value
                Map<String, String> valueMap = new HashMap<String, String>();
                for (int i = 0; i < divisions.length; i++) {
                    String val = divisions[i];
                    valueMap.put(val, val);
                }
                return valueMap;
            }
        });
        departmentField.setEditorType(departmentSelectItem);

        localDataGrid.setFields(employeeField, divisionField, departmentField);

        layout.addMember(localDataGrid);

        //remote dependent selects sample
        Label remoteDataLabel = new Label("Remote Data");
        remoteDataLabel.setWidth("90%");
        remoteDataLabel.setHeight(25);
        remoteDataLabel.setBaseStyle("exampleSeparator");
        layout.addMember(remoteDataLabel);

        final ListGrid remoteDataGrid = new ListGrid();
        remoteDataGrid.setWidth(500);
        remoteDataGrid.setHeight(200);
        remoteDataGrid.setCanEdit(true);

        ListGridField quantityField = new ListGridField("quantity", "Qty");
        quantityField.setType(ListGridFieldType.INTEGER);
        quantityField.setWidth(30);

        ListGridField categoryField = new ListGridField("categoryName", "Category");

        DataSource supplyCategoryDS = SupplyCategoryXmlDS.getInstance();
        DataSource supplyItemDS = ItemSupplyXmlDS.getInstance();

        SelectItem categorySelectItem = new SelectItem();
        categorySelectItem.setOptionDataSource(supplyCategoryDS);

        categoryField.setEditorType(categorySelectItem);

        categoryField.addChangedHandler(new com.smartgwt.client.widgets.grid.events.ChangedHandler() {
            public void onChanged(com.smartgwt.client.widgets.grid.events.ChangedEvent event) {
                remoteDataGrid.clearEditValue(event.getRowNum(), "itemName");
            }
        });

        ListGridField itemField = new ListGridField("itemName", "Item");
        SelectItem itemEditor = new SelectItem();
        itemEditor.setPickListFilterCriteriaFunction(new FilterCriteriaFunction() {
            public Criteria getCriteria() {
                String category = (String) remoteDataGrid.getEditedCell(remoteDataGrid.getEditRow(), "categoryName");
                return new Criteria("category", category);
            }
        });


        itemEditor.setOptionDataSource(supplyItemDS);
        itemField.setEditorType(itemEditor);

        remoteDataGrid.setFields(quantityField, categoryField, itemField);

        layout.addMember(remoteDataGrid);

        IButton newOrderButton = new IButton("Order New Item");
        newOrderButton.setWidth(110);
        newOrderButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Map defaultValues = new HashMap();
                defaultValues.put("quantity", 1);
                remoteDataGrid.startEditingNew(defaultValues);
            }
        });

        layout.addMember(newOrderButton);
        
        layout.draw();
    }

}
</textarea>
<script class='javascript'>
dp.SyntaxHighlighter.HighlightAll("code");
</script>
</body>
</html>
